package cn.tedu.boot04.controller;

import cn.tedu.boot04.entity.User;
import cn.tedu.boot04.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    UserMapper mapper;
    @RequestMapping("/reg")
    public String reg(User user){
        System.out.println("user = " + user);
        User user1=mapper.selectByUsername(user.getUsername());
        if(user1!=null){
            return "<h1 >用户名已存在</h1><a href='/'>返回首页</a>";
        }
        mapper.insert(user);
        return "注册成功";
    }

    @RequestMapping("login")
    public String login(User user){
        System.out.println("user = " + user);
        User user1=mapper.selectBypassword(user.getUsername());
        if(user1==null){
            return "用户名不存在<a href='/login1.html'>返回登录</a>";
        }
        if(user.getPassword().equals(user1.getPassword())) {
            return "登录成功<a href='/login1.html'>返回首页</a>";
        }
        return "登录失败密码错误<a href='/login1.html'>返回登录</a>";
    }

    @RequestMapping("/check")
    public String check(String username) {
        System.out.println("username = " + username);
        User u=mapper.selectByUsername(username);

        return u==null?"用户名可用":"用户名已存在";
    }
}
